package com.wanpinghui.supplier.message.nim;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.netease.nimlib.sdk.msg.attachment.MsgAttachment;
import com.netease.nimlib.sdk.msg.attachment.MsgAttachmentParser;
import com.wanpinghui.wphlibrary.model.message.bean.MessageType;

/**
 * Created by xiongning on 17-12-5.
 */

public class WPHAttachParser implements MsgAttachmentParser {

    private static final String KEY_TYPE = "msg_type";

    private static final String KEY_DATA = "data";

    @Override
    public MsgAttachment parse(String json) {
        WPHAttachment attachment = null;
        try {
            JSONObject object = JSON.parseObject(json);
            int msg_type = object.getInteger("msg_type");
            JSONObject data = object.getJSONObject(KEY_DATA);
            switch (msg_type) {
                case MessageType.TYPE_NORMAL:
                    attachment = new WPHMsg(msg_type);
                    break;
                default:
                    attachment = new WPHMsg(msg_type);
                    break;
            }
            if (attachment != null) {
                attachment.fromJson(data);
            }
        } catch (Exception e) {

        }
        return attachment;
    }

    public static String packData(int type, JSONObject data) {
        JSONObject object = new JSONObject();
        object.put(KEY_TYPE, type);
        if (data != null) {
            object.put(KEY_DATA, data);
        }

        return object.toJSONString();
    }
}
